﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class _100ye : Information
    {
        string mobile, phone, linkman, siteId, site_name, InShop;
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("membername", name);
            pd.Add("password", pass);
            pd.Add("PersistentCookie", "1");
            pd.Add("Login", "1");
            pd.Add("imageField.x", "14");
            pd.Add("imageField.y", "17");
            string html = source.Post("http://member.100ye.com/Manage/Login/Login.asp", pd.Read());
            if (html.IndexOf("<script>window.location.replace('/Manage/checkup.asp');</script>") != -1)
            {
                html = source.Get("http://member.100ye.com/Manage/checkup.asp");
                html = source.Get("http://member.100ye.com/manage/goods_add_info.asp?select_nid=" + site.TypeId[0], "http://member.100ye.com/manage/left.asp");
                mobile = Regex.Match(html, "\"mobile\" value=\"([^\"]+)\"").Groups[1].Value;
                phone = Regex.Match(html, "\"phone\" value=\"([^\"]+)\"").Groups[1].Value;
                linkman = Regex.Match(html, "\"linkman\" value=\"([^\"]+)\"").Groups[1].Value;
                siteId = Regex.Match(html, "\"siteID\" value=\"([^\"]+)\"").Groups[1].Value;
                site_name = Regex.Match(html, "\"site_name\" value=\"([^\"]+)\"").Groups[1].Value;
                InShop = Regex.Match(html, "\"InShop\" value=\"([^\"]+)\"").Groups[1].Value;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            content = MyFunction.ToText(content);
            InformationResult result = new InformationResult();
            string html = source.Get("http://member.100ye.com/manage/goods_offten_class.asp");
            if (html.IndexOf("您发布的信息量已超过您的信息空间总量") != -1)
            {
                result.Code = 2;
                return result;
            }
            ParametersData pd = new ParametersData("GB2312");
            pd.Add("goods_name", title);
            pd.Add("site_name", site_name);
            pd.Add("siteID", siteId);
            pd.Add("select_nid", site.TypeId[0]);
            pd.Add("pinpai", "0");
            pd.Add("price", "0");
            pd.Add("Body", content);
            pd.Add("countbox", "7");
            pd.Add("Service", "2");
            pd.Add("Submit", "提交");
            pd.Add("issubmit", "1");
            pd.Add("InShop", InShop);
            pd.Add("linkman", linkman);
            pd.Add("phone", phone);
            pd.Add("mobile", mobile);
            pd.Add("email", "");
            html = source.Post("http://member.100ye.com/manage/goods_add_process.asp?sign=2451", pd.Read());
            if (html.IndexOf("confirm('信息已经发布成功,你是否要上传图片") != -1)
            {
                html = source.Get("http://member.100ye.com/manage/goods_list.asp");
                string url = Regex.Match(html, "href=\"([^\"]+)\" target=\"_blank\" title=" + title).Groups[1].Value;
                result.Code = 1;
                result.Content = url;
                UpdateImage(Regex.Match(url, "([0-9]+)\\.html").Groups[1].Value);
            }
            else if (Regex.IsMatch(html, "<script>alert\\('([^']+)'"))
            {
                result.Content = Regex.Match(html, "<script>alert\\('([^']+)'").Groups[1].Value;
            }
            return result;
        }

        private void UpdateImage(string id)
        {
            string html = source.Get("http://100yeuserfiles.100ye.com/DefaultUp.aspx?goods_id=" + id, "http://100yeuserfiles.100ye.com/DefaultUp.aspx?goods_id=" + id);
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("__EVENTVALIDATION", Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)\"").Groups[1].Value);
            //发送文件
            sfd.WriteFile("FileUpload1", RandomPicture.Get());
            //尾部数据
            sfd.Write("Button1", "上传");
            //结尾
            html = source.Post("http://100yeuserfiles.100ye.com/DefaultUp.aspx?goods_id=" + id, sfd.GetContentType(), sfd.Read(), "http://100yeuserfiles.100ye.com/DefaultUp.aspx?goods_id=" + id);
        }
    }
}
